package com.only4play.practice;



import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;
/**
 * @author mdy
 * @link <a href="https://mp.baomidou.com/guide/generator.html">...</a>
 * * @date 2024/9/13
 */
public class MyBatisPlusCodeGenerator {
    // 要生成的表名
    private static final String[] TABLE_NAMES = {"enterprise_entity","enterprise_router",
            "invoice","invoice_item","order_receipt","pay_item_config","receipt_invoice_rel"
            ,"receipt_item","tax_rate_config"};
    private static final String[] TABLE_PREFIXES = {"t_"};
    // 生成代码包的基础路径
    private static final String BASE_PACKAGE_URL =
"C:\\Users\\MDY\\Desktop\\tmp-code\\practice-system\\practice-invoice-center\\practice-invoice-core\\src\\main\\java";
    // xml文件路径 取绝对路径
    private static final String XML_PACKAGE_URL =
"C:\\Users\\MDY\\Desktop\\tmp-code\\practice-system\\practice-invoice-center\\practice-invoice-core\\src\\main\\resources\\mappers"

            ;
    private static final String BASE_PACKAGE_NAME = "com.only4play.practice.invoice";
    private static final String PARENT_MODULE_NAME = "core";
    private static final String HOST = "192.168.56.140";
    private static final String DATABASE = "practice-system-center";
    private static final String AUTHOR = "mdy";
    // 数据库 URL
    private static final String URL = "jdbc:mysql://" + HOST + ":3306/" + DATABASE +"?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    // 数据库驱动
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    // 数据库用户名
    private static final String USERNAME = "root";
    // 数据库密码
    private static final String PASSWORD = "root";
    // @author 值

//    下面这些模板文件取相对路径，因为在generator项目
    // xml 文件模板
    private static final String XML_MAPPER_TEMPLATE_PATH = "templates/mapper.xml";
    // mapper 文件模板
    private static final String MAPPER_TEMPLATE_PATH = "templates/mapper.java";
    // entity 文件模板
    private static final String ENTITY_TEMPLATE_PATH = "templates/entity.java";
    // service 文件模板
    private static final String SERVICE_TEMPLATE_PATH = "templates/service.java";
    // serviceImpl 文件模板
    private static final String SERVICE_IMPL_TEMPLATE_PATH = "templates/serviceImpl.java";
    // controller 文件模板
    private static final String CONTROLLER_TEMPLATE_PATH = "templates/controller.java";

    public static void main(String[] args) {
        // 注意代码生成器依赖的版本问题
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)

                // 全局配置
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(BASE_PACKAGE_URL); // 指定输出目录
                })

                // 数据源配置
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )

                // 包配置
                .packageConfig(builder ->
                        // 设置父包名
                        builder.parent(BASE_PACKAGE_NAME)
                                // 设置父包模块名
                                .moduleName(PARENT_MODULE_NAME)
                                // 设置entity包名
                                .entity("entity")
                                // 设置service包名
                                .service("service")
                                // 设置serviceImpl包名
                                .serviceImpl("service.impl")
                                // 设置controller包名
                                .controller("controller")
                                // 设置mapper包名
                                .mapper("mapper")
                                // 设置mapper.xml包名
                                .xml("mappers")
                                // 设置mapper.xml保存路径
                                .pathInfo(Collections.singletonMap(OutputFile.xml, XML_PACKAGE_URL))

                )
                // 配置自定义代码模板
                .templateConfig(builder -> {
                    builder
                            .entity(ENTITY_TEMPLATE_PATH)
                            .service(SERVICE_TEMPLATE_PATH)
                            .serviceImpl(SERVICE_IMPL_TEMPLATE_PATH)
                            .controller(CONTROLLER_TEMPLATE_PATH)
                            .mapper(MAPPER_TEMPLATE_PATH)
                            .xml(XML_MAPPER_TEMPLATE_PATH)
                    ;
                })

                // 策略配置
                .strategyConfig(builder ->
                        // 设置需要生成的表名
                        builder.addInclude(TABLE_NAMES)
                                // 设置过滤表前缀
                                .addTablePrefix(TABLE_PREFIXES)
                                .entityBuilder()
                                    // entity 开启lombok模式
                                    .enableLombok()
                                .controllerBuilder()
                                    // 启用 REST 风格
                                    .enableRestStyle()
                )
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}

